//
//  MainViewController.swift
//  新浪微博
//
//  Created by macbook on 16/3/6.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        //创建一个tabbar
        let mainTabBar = MainTabBar()
        
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        print(tabBar.classForCoder)
        
        //给对象提供类似字典的方式访问对象属性，系统tabbar是只读属性，不能直接操作
        setValue(mainTabBar, forKey: "tabBar")
        
        print(tabBar.classForCoder)
        
        
       //添加所有子视图控制器
        addChildViewControllers()
     }
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName:"tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName:"tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName:"tabbar_profile")
        
        
    }
        
    
    
    
    //添加子视图控制器
    private func addChildViewController(vc: UIViewController,title:String,imageName:String) {
        
        //设置title 上面的标题
        vc.title = title
        //下面的标题
        vc.tabBarItem.title = title
        
       tabBar.tintColor = UIColor.orangeColor()
        
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置选中的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")?.imageWithRenderingMode(.AlwaysOriginal)//保持原样不渲染
        
        
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    
}
